package test04;

import java.util.ArrayList;
import java.util.List;

// 视频类
class Video {
    private String title;
    private String format;
    private String content;
    private boolean hasCopyright;
    private boolean isHighQuality;

    public Video(String title, String format, String content,
                 boolean hasCopyright, boolean isHighQuality) {
        this.title = title;
        this.format = format;
        this.content = content;
        this.hasCopyright = hasCopyright;
        this.isHighQuality = isHighQuality;
    }

    // Getters
    public String getTitle() { return title; }
    public String getFormat() { return format; }
    public String getContent() { return content; }
    public boolean hasCopyright() { return hasCopyright; }
    public boolean isHighQuality() { return isHighQuality; }

    @Override
    public String toString() {
        return "《" + title + "》[格式:" + format + ", 版权:" + hasCopyright +
                ", 高质量:" + isHighQuality + "]";
    }
}

// 抽象审核处理器
abstract class AuditHandler {
    protected AuditHandler nextHandler;

    public void setNextHandler(AuditHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    // 处理审核请求
    public final boolean handleRequest(Video video) {
        System.out.println("🔍 进入 " + this.getClass().getSimpleName() + " 审核...");

        // 执行当前审核
        boolean passed = audit(video);

        if (!passed) {
            System.out.println("❌ " + this.getClass().getSimpleName() + " 审核不通过");
            return false;
        }

        System.out.println("✅ " + this.getClass().getSimpleName() + " 审核通过");

        // 传递给下一个处理器
        if (nextHandler != null) {
            return nextHandler.handleRequest(video);
        }

        // 所有审核都通过
        System.out.println("🎉 所有审核环节通过，视频可以发布！");
        return true;
    }

    // 具体的审核逻辑，由子类实现
    protected abstract boolean audit(Video video);
}

// 格式审核处理器
class FormatAuditHandler extends AuditHandler {
    private static final List<String> SUPPORTED_FORMATS =
            List.of("mp4", "avi", "mov", "wmv");

    @Override
    protected boolean audit(Video video) {
        String format = video.getFormat().toLowerCase();
        if (!SUPPORTED_FORMATS.contains(format)) {
            System.out.println("  格式不支持: " + format + "，支持格式: " + SUPPORTED_FORMATS);
            return false;
        }
        System.out.println("  格式检查通过: " + format);
        return true;
    }
}

// 内容安全审核处理器
class ContentSafetyAuditHandler extends AuditHandler {
    private static final List<String> SENSITIVE_WORDS =
            List.of("暴力", "色情", "违法", "反动", "诈骗");

    @Override
    protected boolean audit(Video video) {
        String content = video.getContent();

        for (String word : SENSITIVE_WORDS) {
            if (content.contains(word)) {
                System.out.println("  内容包含敏感词: " + word);
                return false;
            }
        }

        System.out.println("  内容安全检查通过");
        return true;
    }
}

// 版权审核处理器
class CopyrightAuditHandler extends AuditHandler {
    @Override
    protected boolean audit(Video video) {
        if (!video.hasCopyright()) {
            System.out.println("  版权审核不通过：视频可能存在版权问题");
            return false;
        }
        System.out.println("  版权审核通过");
        return true;
    }
}

// 推荐审核处理器
class RecommendationAuditHandler extends AuditHandler {
    @Override
    protected boolean audit(Video video) {
        if (!video.isHighQuality()) {
            System.out.println("  推荐审核不通过：视频质量不符合推荐标准");
            return false;
        }
        System.out.println("  推荐审核通过 - 视频符合推荐标准");
        return true;
    }
}

// 审核系统
class AuditSystem {
    private AuditHandler auditChain;
    private List<AuditHandler> handlers;

    public AuditSystem() {
        this.handlers = new ArrayList<>();
        initializeDefaultChain();
    }

    private void initializeDefaultChain() {
        // 创建默认审核链：格式 → 内容安全 → 版权 → 推荐
        FormatAuditHandler formatHandler = new FormatAuditHandler();
        ContentSafetyAuditHandler contentHandler = new ContentSafetyAuditHandler();
        CopyrightAuditHandler copyrightHandler = new CopyrightAuditHandler();
        RecommendationAuditHandler recommendationHandler = new RecommendationAuditHandler();

        formatHandler.setNextHandler(contentHandler);
        contentHandler.setNextHandler(copyrightHandler);
        copyrightHandler.setNextHandler(recommendationHandler);

        this.auditChain = formatHandler;

        // 保存所有处理器用于动态管理
        handlers.add(formatHandler);
        handlers.add(contentHandler);
        handlers.add(copyrightHandler);
        handlers.add(recommendationHandler);
    }

    // 处理视频审核
    public boolean processVideo(Video video) {
        System.out.println("\n📹 开始审核视频: " + video);
        boolean result = auditChain.handleRequest(video);
        System.out.println("最终审核结果: " + (result ? "通过 ✅" : "拒绝 ❌"));
        return result;
    }

    // 动态添加处理器
    public void addHandler(AuditHandler newHandler) {
        if (handlers.isEmpty()) {
            auditChain = newHandler;
        } else {
            AuditHandler lastHandler = handlers.get(handlers.size() - 1);
            lastHandler.setNextHandler(newHandler);
        }
        handlers.add(newHandler);
        System.out.println("添加审核处理器: " + newHandler.getClass().getSimpleName());
    }

    // 动态移除处理器
    public void removeHandler(Class<? extends AuditHandler> handlerType) {
        if (handlers.size() <= 1) {
            System.out.println("不能移除唯一的处理器");
            return;
        }

        for (int i = 0; i < handlers.size(); i++) {
            if (handlerType.isInstance(handlers.get(i))) {
                AuditHandler removed = handlers.remove(i);

                // 重新构建链
                rebuildChain();
                System.out.println("移除审核处理器: " + removed.getClass().getSimpleName());
                return;
            }
        }
    }

    // 重新构建责任链
    private void rebuildChain() {
        if (handlers.isEmpty()) {
            auditChain = null;
            return;
        }

        for (int i = 0; i < handlers.size() - 1; i++) {
            handlers.get(i).setNextHandler(handlers.get(i + 1));
        }
        handlers.get(handlers.size() - 1).setNextHandler(null);

        auditChain = handlers.get(0);
    }

    // 设置自定义审核链
    public void setAuditChain(AuditHandler chain) {
        this.auditChain = chain;
        // 更新handlers列表
        handlers.clear();
        AuditHandler current = chain;
        while (current != null) {
            handlers.add(current);
            current = current.nextHandler;
        }
    }

    public void printAuditChain() {
        System.out.println("\n当前审核链:");
        AuditHandler current = auditChain;
        int index = 1;
        while (current != null) {
            System.out.println(index + ". " + current.getClass().getSimpleName());
            current = current.nextHandler;
            index++;
        }
    }
}

// 演示类
public class DouyinVideoAuditSystem {
    public static void main(String[] args) {
        System.out.println("=== 抖音视频审核系统演示 ===\n");

        // 创建审核系统
        AuditSystem auditSystem = new AuditSystem();
        auditSystem.printAuditChain();

        // 创建测试视频
        Video video1 = new Video("美丽的风景", "mp4", "这是一个展示自然风光的视频", true, true);
        Video video2 = new Video("测试视频", "flv", "普通内容视频", true, false);
        Video video3 = new Video("敏感内容", "mp4", "这个视频包含违法内容", true, true);
        Video video4 = new Video("盗版电影", "avi", "最新上映电影", false, true);
        Video video5 = new Video("高质量教程", "mp4", "编程教学视频", true, true);

        System.out.println("\n=== 测试场景1: 完全合规视频 ===");
        auditSystem.processVideo(video1);

        System.out.println("\n=== 测试场景2: 格式不支持 ===");
        auditSystem.processVideo(video2);

        System.out.println("\n=== 测试场景3: 内容安全审核不通过 ===");
        auditSystem.processVideo(video3);

        System.out.println("\n=== 测试场景4: 版权审核不通过 ===");
        auditSystem.processVideo(video4);

        System.out.println("\n=== 测试场景5: 推荐审核不通过 ===");
        auditSystem.processVideo(new Video("普通视频", "mp4", "日常记录", true, false));

        System.out.println("\n=== 动态调整审核链演示 ===");

        // 移除推荐审核（某些场景下不需要推荐审核）
        auditSystem.removeHandler(RecommendationAuditHandler.class);
        auditSystem.printAuditChain();

        System.out.println("\n=== 移除推荐审核后重新测试 ===");
        auditSystem.processVideo(video5);

        System.out.println("\n=== 添加水印审核处理器 ===");

        // 动态添加新的审核环节
        AuditHandler watermarkHandler = new AuditHandler() {
            @Override
            protected boolean audit(Video video) {
                System.out.println("  水印检查通过");
                return true;
            }
        };
        auditSystem.addHandler(watermarkHandler);
        auditSystem.printAuditChain();

        System.out.println("\n=== 新审核链测试 ===");
        auditSystem.processVideo(video1);

        System.out.println("\n=== 责任链模式优势总结 ===");
        System.out.println("1. 解耦特性: 审核处理器之间相互独立，无需知道彼此的存在");
        System.out.println("2. 灵活性: 可以动态添加、移除或调整审核环节顺序");
        System.out.println("3. 可扩展性: 新增审核类型只需创建新的处理器类");
        System.out.println("4. 单一职责: 每个处理器只关注自己的审核逻辑");
        System.out.println("5. 开放封闭: 对扩展开放，对修改封闭");
    }
}
